//
//  MXStatusRetweetView.swift
//  weibo
//
//  Created by 孙飞 on 15/12/12.
//  Copyright © 2015年 Sunfei. All rights reserved.
//

import UIKit
import SnapKit

class MXSStatusRetweetView: UIView {

    // 当前视图的底部约束
    var bottomConstraint: Constraint?
    
    var statusViewModel: MXSStatusVM? {
        didSet {
            contentLabel.text = statusViewModel?.retweetText
            
            //如果有图
            //******** 1. 让其显示出来;2.更新约束;3.设置数据
            
            self.bottomConstraint?.uninstall()
            if let pic_urls = statusViewModel?.status?.retweeted_status?.pic_urls where pic_urls.count > 0 {
                // 设置数据
                pictureView.pic_urls = pic_urls
                // 显示
                pictureView.hidden = false
                // 更新约束
                snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(pictureView).offset(MXSCellMargin).constraint
                })
            }else{
                // 1. 让其隐藏;2.更新约束
                pictureView.hidden = true
                // 2. 更新约束
                snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(contentLabel).offset(MXSCellMargin).constraint
                })
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 设置视图
    private func setupUI(){
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        // 添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        
        // 添加约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(MXSCellMargin)
            make.top.equalTo(self).offset(MXSCellMargin)
        }
        // 配图的约束
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(MXSCellMargin)
        }
        
        // 当前控件的高与内容的高度一样的
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstraint = make.bottom.equalTo(pictureView).offset(MXSCellMargin).constraint
        }
    }
    
    
    // MARK: - 懒加载
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15, maxWidth: SCREENW - 2 * MXSCellMargin)
    
    // 配图View
    private lazy var pictureView: MXSStatusPicView = {
        let view = MXSStatusPicView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
    
}

